package com.ymatou.iapi.godmch.testcase;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.common.collect.Lists;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.applySupplierBean;
import com.ymatou.iapi.godmch.parameter.bindmchBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.getAuthRelationBean;
import com.ymatou.iapi.godmch.service.CreateCodeCall;
import com.ymatou.iapi.godmch.service.applyAgentCall;
import com.ymatou.iapi.godmch.service.applySupplierCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.getAuthRelationCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.shop_design_auth;
import com.ymttest.database.model.godmch.shop_design_authExample;
import com.ymttest.database.sqlwapper.godmch.shop_design_authWapper;
/**
*查询商户授权(上级查下级)
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getAuthRelation{
	private static getAuthRelationBean bean;
	private static getAuthRelationCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询商户授权(上级查下级)");
	}

	@Before
	public void caseUp() {
		bean=new getAuthRelationBean();
		call=new getAuthRelationCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void check_verify() throws InterruptedException, JSONException
	{
	    Logger.verifyEquals("200", call.getCode(), "验证返回Code");
	    
		shop_design_authWapper shop_design_authWapper=new shop_design_authWapper();
		shop_design_authExample example=new shop_design_authExample();
		example.createCriteria().andMch_idIn(bean.getMchIdList()).andParent_mch_idEqualTo(bean.getParentMchId());
		List<shop_design_auth> shop=shop_design_authWapper.selectByExample(example);
		
	    JSONArray jArray=call.getList();
	    for(int i=0;i<jArray.length();i++)
	    {
	    	Integer return_obj=Integer.parseInt(jArray.get(i).toString());
	    	Logger.verifyEquals(shop.get(i).getMch_id(), return_obj, "验证第"+(i+1)+"条数据的mcId");
	    	//Logger.verifyEquals(shop.get(i).getAuth_state()==0?true:false, return_obj.getBoolean("authorized"), "验证第"+(i+1)+"条数据的authorized");
	    }
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAuthRelation_001() {
		Logger.start(true,"查询商户授权(上级查下级)-平台商户-授权-返回成功");
		try {
			Integer parentMchId=GodmchManageCallService.CreateSellerMch();
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			//渠道商1邀请码
			CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(parentMchId);
			CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
			//绑定关系
			bindmchBean bindmchBean=GodmchManageCallService.InitbindmchBean(mchId, createcall.getCode());
			GodmchManageCallService.bindmchcall(bindmchBean);
			GodmchManageCallService.authShopDesign(mchId,parentMchId);
			
			bean.setMchIdList(Lists.newArrayList(mchId));
			bean.setParentMchId(parentMchId);
			call.setData(bean);
			call.callService();			

			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAuthRelation_001_1() {
		Logger.start(true,"查询商户授权(上级查下级)-平台商户-未授权-返回成功");
		try {
			Integer parentMchId=GodmchManageCallService.CreateSellerMch();
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			//渠道商1邀请码
			CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(parentMchId);
			CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
			//绑定关系
			bindmchBean bindmchBean=GodmchManageCallService.InitbindmchBean(mchId, createcall.getCode());
			GodmchManageCallService.bindmchcall(bindmchBean);
		//	GodmchManageCallService.authShopDesign(mchId,parentMchId);
			
			bean.setMchIdList(Lists.newArrayList(mchId));
			bean.setParentMchId(parentMchId);
			call.setData(bean);
			call.callService();			
		    Logger.verifyEquals("200", call.getCode(), "验证返回Code");
		    Logger.verifyEquals(true, call.getList().length()==0, "验证返回Result");
		    
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAuthRelation_001_2() {
		Logger.start(true,"查询商户授权(上级查下级)-平台商户-取消授权-返回空(只返回授权的有效数据)");
		try {
			Integer parentMchId=GodmchManageCallService.CreateSellerMch();
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			//渠道商1邀请码
			CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(parentMchId);
			CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
			//绑定关系
			bindmchBean bindmchBean=GodmchManageCallService.InitbindmchBean(mchId, createcall.getCode());
			GodmchManageCallService.bindmchcall(bindmchBean);
			//授权
			GodmchManageCallService.authShopDesign(mchId,parentMchId);
			//取消授权
			GodmchManageCallService.cancelAuthShopDesign(mchId,parentMchId);
			
			bean.setMchIdList(Lists.newArrayList(mchId));
			bean.setParentMchId(parentMchId);
			call.setData(bean);
			call.callService();			

		    Logger.verifyEquals("200", call.getCode(), "验证返回Code");
		    Logger.verifyEquals(true, call.getList().length()==0, "验证返回Result");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAuthRelation_002() {
		Logger.start(true,"查询商户授权(上级查下级)-独立商户-返回成功");
		try {
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
			applyAgentCall applyAgentCall=GodmchManageCallService.applyAndapproveAgent(createIndepMchCall.getmchId());
			
			//授权装修
			GodmchManageCallService.authShopDesign( applyAgentCall.getAgentId(),createIndepMchCall.getmchId());
			
			bean.setMchIdList(Lists.newArrayList(applyAgentCall.getAgentId()));
			bean.setParentMchId(createIndepMchCall.getmchId());
			call.setData(bean);
			call.callService();	
			
			check_verify(); 
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAuthRelation_003() {
		Logger.start(true,"查询商户授权(上级查下级)-独立商户-供货商-返回成功");
		try {
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
			//申请供货商
			applySupplierBean applySupplierBean=GodmchManageCallService.initApplySupplier(createIndepMchCall.getmchId());
			applySupplierCall applySupplierCall=GodmchManageCallService.applySupplierCall(applySupplierBean);
			//审核
			GodmchManageCallService.approveSupplier(createIndepMchCall.getmchId(), applySupplierCall.getSupplierId());
			
			//授权装修
			GodmchManageCallService.authShopDesign(createIndepMchCall.getmchId(),applySupplierCall.getSupplierId());
			
			bean.setMchIdList(Lists.newArrayList(createIndepMchCall.getmchId()));
			bean.setParentMchId(applySupplierCall.getSupplierId());
			call.setData(bean);
			call.callService();	
			
			check_verify(); 
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAuthRelation_004() {
		Logger.start(true,"查询商户授权(上级查下级)-上下级关系不存在-返回成功");
		try {
			Integer parentMchId=GodmchManageCallService.CreateSellerMch();
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			//渠道商1邀请码
			CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(parentMchId);
			CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
			//绑定关系
			bindmchBean bindmchBean=GodmchManageCallService.InitbindmchBean(mchId, createcall.getCode());
			GodmchManageCallService.bindmchcall(bindmchBean);
			GodmchManageCallService.authShopDesign(mchId,parentMchId);
			
			bean.setMchIdList(Lists.newArrayList(parentMchId));
			bean.setParentMchId(mchId);
			call.setData(bean);
			call.callService();			
			
		    Logger.verifyEquals("200", call.getCode(), "验证返回Code");
		    Logger.verifyEquals(true, call.getList().length()==0, "验证返回Result");
		    
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAuthRelation_005() {
		Logger.start(true,"查询商户授权(上级查下级)-商户id为空-返回成功");
		try {
			Integer parentMchId=GodmchManageCallService.CreateSellerMch();
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			//渠道商1邀请码
			CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(parentMchId);
			CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
			//绑定关系
			bindmchBean bindmchBean=GodmchManageCallService.InitbindmchBean(mchId, createcall.getCode());
			GodmchManageCallService.bindmchcall(bindmchBean);
			GodmchManageCallService.authShopDesign(mchId,parentMchId);
			
			//bean.setMchIdList(Lists.newArrayList(mchId));
			bean.setParentMchId(parentMchId);
			call.setData(bean);
			call.callService();		
			
		    Logger.verifyEquals("4001", call.getCode(), "验证返回Code");
		    
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAuthRelation_006() {
		Logger.start(true,"查询商户授权(上级查下级)-上级商户id为空-返回成功");
		try {
			Integer parentMchId=GodmchManageCallService.CreateSellerMch();
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			//渠道商1邀请码
			CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(parentMchId);
			CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
			//绑定关系
			bindmchBean bindmchBean=GodmchManageCallService.InitbindmchBean(mchId, createcall.getCode());
			GodmchManageCallService.bindmchcall(bindmchBean);
			GodmchManageCallService.authShopDesign(mchId,parentMchId);
			
			bean.setMchIdList(Lists.newArrayList(mchId));
			//bean.setParentMchId(parentMchId);
			call.setData(bean);
			call.callService();		
			
		    Logger.verifyEquals("4001", call.getCode(), "验证返回Code");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAuthRelation_007() {
		Logger.start(true,"查询商户授权(上级查下级)-MchIdList集合含不存在的商户授权关系-返回成功");
		try {
			
			Integer parentMchId=GodmchManageCallService.CreateSellerMch();
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			Integer mchId2=GodmchManageCallService.CreateSellerMch();
			//渠道商1邀请码
			CreateCodeBean createbean=GodmchManageCallService.InitcreateCodeBeanForAgent(parentMchId);
			CreateCodeCall createcall=GodmchManageCallService.createCodeCall(createbean);
			//绑定关系
			bindmchBean bindmchBean=GodmchManageCallService.InitbindmchBean(mchId, createcall.getCode());
			GodmchManageCallService.bindmchcall(bindmchBean);
			GodmchManageCallService.authShopDesign(mchId,parentMchId);
			
			bean.setMchIdList(Lists.newArrayList(mchId,mchId2));
			bean.setParentMchId(parentMchId);
			call.setData(bean);
			call.callService();		
			
			check_verify(); 
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}